// pages/order/confirm/confirm.js
const utils = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    curriculum: [],
    total: 0,
    id: 0,
    cid: 0,
    coupon_price: 0,
    payDisabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    let id = options.id;
    let cid = options.cid;
    let price = options.price;
    _this.setData({
      id: id,
      cid: cid,
      coupon_price: price
    })
    utils.requests({
      url: 'curriculum/order/'+id,
      success: function(data){
        let total = price>0?data.data.data.price-price:data.data.data.price;
        _this.setData({
          curriculum:data.data.data,
          total: total
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 页面跳转
  jump: function(e) {
    let url = e.currentTarget.dataset.url;
    let id = e.currentTarget.dataset.id;
    let price = e.currentTarget.dataset.price;
    wx.navigateTo({
      url: url+(id>0?'?id='+id:'')+(price?'&price='+price:''),
    })
  },

  // 关闭当前页面跳转
  jumpRedirect: function(e){
    let url = e.currentTarget.dataset.url;
    let id = e.currentTarget.dataset.id;
    let price = e.currentTarget.dataset.price;
    wx.redirectTo({
      url: url+(id>0?'?id='+id:'')+(price?'&price='+price:''),
    })
  },

  // 支付
  payment: function(e) {
    let _this = this;
    _this.setData({
      payDisabled: true
    })
    let data = {};
    data.id = _this.data.id;
    data.cid = _this.data.cid;
    data.total = _this.data.total;
    utils.requests({
      url: 'order/set',
      method: 'post',
      data:data,
      success:function(data){
        if ( data.data.code == 200 ) {
          wx.navigateTo({
            url: '../../mine/order/list/list',
          })
          return false;
          let res = {};
          res = data.data.data;
          res.success = function(data){
            wx.navigateTo({
              url: '../../mine/order/list/list',
            })
          };
          res.fail = function(data){
            wx.showToast({
              title: '微信支付发起失败',
              icon: 'none',
              duration: 1500
            })
          };
          res.complete = function(data){
            if ( data.errMsg === 'requestPayment:fail cancel' ) {
              wx.showToast({
                title: '您已取消支付',
                icon: 'none',
                duration: 1500
              })
            }
          }
          wx.requestPayment(res);
        }
      },
      complete: data.complete || function(data){
        if ( data.data.code != 200 ) {
          wx.showToast({
            title: data.data.message,
            icon: 'none',
            duration: 1500
          })
        } else {
          _this.setData({
            payDisabled: false
          })
        }
      }
    })
  }

})